const fs = require('fs');
const _ = require('lodash');

const pageFiles = fs.readdirSync('./src/pages');


// 所有页面
// const allPages = {
//   about: {
//     entry: './src/pages/about/about.js',
//     template: './src/pages/about/about.html',
//     filename: 'about.html',
//     title: '关于',
//     // chunks: ['chunk-vendors', 'chunk-common', 'about'],
//   },
//   test: {
//     entry: './src/pages/test/main.js',
//     template: './src/pages/test/test.html',
//     filename: 'test.html',
//     title: '测试',
//   },
// };
const allPages = {};
pageFiles.forEach(function(d) {
  allPages[d] = {
    entry: `./src/pages/${d}/${d}.js`,
    template: `./src/pages/${d}/${d}.html`,
    filename: `${d}.html`,
  }
});

const targetList = process.argv.slice(3);
let currentPages = allPages;
if (targetList.length === 0) {
  console.log('---------- run all pages!')
} else {
  let somePages = {};
  currentPages = _.reduce(allPages, function(r, v, k) {
    if (targetList.includes(k)) {
        r[k] = v;
        console.log(`---------- run ${k}`)
    }
    return r;
  }, {});
  // error
  if (JSON.stringify(currentPages) === '{}') {
    console.log('---------- not found pages!')
    throw new Error('NOT-FOUND')
  }
}

// console.log(currentPages)
// process.exit(0);
module.exports = {
  // indexPath: 'about.html',
  pages: currentPages,
  devServer: {
    port: 8081,
    proxy: {
      '': {
        target: 'http://192.168.1.190',
      },
    },
    historyApiFallback: {
      rewrites: [
        { from: /\/about\//, to: '/about.html' },
        { from: /\/test\//, to: '/test.html' },
      ]
    }
  },
  // baseUrl: '/zh-cn/',
  filenameHashing: false, // 去掉文件名hash后缀
}